// pages/city/city.js
const app = getApp();
import request from '../../../utils/http.js'
// var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
// var qqmapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab:0,
    isOpen: false,//重新定位开关，避免多次重复触发
    city: '',
    cityList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.localAction();
    // this.getCityLit();
    this.getCityList();

    this.setData({
      city: wx.getStorageSync(app.globalData.CityName),
    })
  },


 //获取陪诊开通的城市 
 getCityList: function () {
  var that = this
  let data = {
  }
  request.get('peiZhenProduct/getCityList', null, res => {
    console.log("获取陪诊开通的城市 :" + JSON.stringify(res.data))
    wx.setStorageSync(app.globalData.CityCode, res.data.defaultCityCode)
    wx.setStorageSync(app.globalData.CityName, res.data.defaultCityName)
    that.setData({
      cityList:res.data.listProvince,
    })
  }, err => {
    wx.showToast({
      title: '获取信息失败', // 提示的内容
      icon: "none", // 图标，默认success
      duration: 1000, // 提示的延迟时间，默认1500
      mask: false, // 是否显示透明蒙层，防止触摸穿透
    })
  })

},






  //获取城市list
  // getCityLit(){
  //   var that=this
  //   wx.showLoading({
  //       title: '获取中...',
  //       mask: true
  //   })
  //   request.get('city/provinceList', null, res => {
  //   //   console.log("登录:" + JSON.stringify(res))

  //     if(res.statusCode==200){
  //        that.setData({
  //         cityList:res.data.provinceList
  //        })

  //     }
 
  //    }, err => {
  //     // console.log("ree")
  //    }, complete => {
  //       // console.log("完成")
  //       wx.hideLoading()
  //    })

  // },

  //重新定位
  localAction() {

    if (!this.data.isOpen) {
      let vm = this;
      vm.setData({
        isOpen: true
      })
      qqmapsdk = new QQMapWX({
        key: app.globalData.QQMapKey //自己的key秘钥 
      });

      wx.getSetting({
        success: (res) => {
          // console.log("设置信息"+JSON.stringify(res))
          if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
            wx.showModal({
              title: '请求授权当前位置',
              content: '需要获取您的地理位置，请确认授权',
              success: function (res) {
                if (res.cancel) {
                  wx.showToast({
                    title: '拒绝授权',
                    icon: 'none',
                    duration: 1000
                  })
                } else if (res.confirm) {
                  wx.openSetting({
                    success: function (dataAu) {
                      if (dataAu.authSetting["scope.userLocation"] == true) {
                        wx.showToast({
                          title: '授权成功',
                          icon: 'success',
                          duration: 1000
                        })
                        //再次授权，调用wx.getLocation的API
                        vm.getLocation();
                      } else {
                        wx.showToast({
                          title: '授权失败',
                          icon: 'none',
                          duration: 1000
                        })
                      }
                    }
                  })
                }
              }
            })
          } else if (res.authSetting['scope.userLocation'] == undefined) {
            //调用wx.getLocation的API
            vm.getLocation();
          } else {
            //调用wx.getLocation的API
            vm.getLocation();
          }
        }
      })
    }
  },

  // 微信获得经纬度
  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        // console.log("aaaa:"+JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        // var speed = res.speed
        // var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        // wx.showToast({
        //   title: '获取经纬度a',
        //   icon: 'none',
        //   duration: 1000
        // })
        vm.setData({
          isOpen: false
        })
      }
    })
  },
  // 获取当前地理位置
  getLocal: function (latitude, longitude) {

    let vm = this;

    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        // console.log(JSON.stringify(res))
        // //   let province = res.result.ad_info.province
        // //   let city = res.result.ad_info.city
        // console.log(res.result.ad_info.city_code)

        wx.setStorageSync(app.globalData.CityCode, res.result.ad_info.city_code)
        wx.setStorageSync(app.globalData.CityName, res.result.ad_info.city)
        vm.setData({
          city: res.result.ad_info.city,
          isOpen: false
        })
        let pages = getCurrentPages(); //定义上页面
        // console.log("pages.length:"+pages.length)
        let prevPage = pages[ pages.length - 2 ];
        prevPage.setData({  
          city: res.result.ad_info.city,
          cityCode:res.result.ad_info.city_code
        })

      },
      fail: function (res) {
        // console.log('fail' + JSON.stringify(res))
        vm.setData({
          isOpen: false
        })
      }
    });
  },

//切换城市
  changeCity:function(e){
    var cityName=e.currentTarget.dataset.n;
    var cityCode=e.currentTarget.dataset.c;
    console.log(cityName)
    console.log(cityCode)
    wx.setStorageSync(app.globalData.CityCode, cityCode)
    wx.setStorageSync(app.globalData.CityName, cityName)

    let pages = getCurrentPages(); //定义上页面
    // console.log("pages.length:"+pages.length)
    let prevPage = pages[ pages.length - 2 ];
    prevPage.setData({  
      city:cityName,
      cityCode:cityCode,
    })

    wx.navigateBack({
      delta: 1,
  })
  },

  swichNav: function (e) {
    var index = e.target.dataset.index;
    if (this.data.currentTab == index) {
      return false;
    } else {
      this.setData({
        currentTab: index
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // wx.setStorageSync(app.globalData.ChangeCity, true)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
        title: app.globalData.Sharetitle,
        imageUrl:app.globalData.ShareimageUrl,
        path: '/pages/index/index?tjrId=' + wx.getStorageSync(app.globalData.PersonId) 
    }
  }
})